// pages/dgame1/dgame1.js
const tubiao = 'https://www.juquduo.com/static/resourse/dianwei/顺序框1.png';
const gamebg = 'https://www.juquduo.com/static/resourse/yeshou/地图1.jpg'
var list = [];
var all = [];
var alldd = [];
var mks = [];
var aalength;
var openid, xsarr, fanwei, groupid = '',
  roomid = '',
  fenzu;
var app = getApp()
var pointindex = 0;
var pointindextmp = 0;
var temppid = 0;
var xiansuo11 = 1;
var panduan = 1;
var sitesite, sdsdid, iaaa, sitetype, loadurl = '';
var anniunext = false;
var _locationChangeFn;
var qingjie
var num = 0
var numm = 0
Page({
  data: {
    qiehuan: '../image/game4/tanxian22.png',
    xiansuo: 'https://www.juquduo.com/static/resourse/game4/xiansuo.png',
    isshowbtn: false,
    showmap: true,
    isshowxiansuo2: false,
    isshowxiansuo22: false,
    showzhankuang: false,
    roomscore: false,
    modalHidden: true,
    xiansuocontent: '',
    anniunext: true,
    modalmons: true,
    xsarr: [],
    sitetype: '',
    all: [],
    showrrr: false,
    showbotbar: true,
    game1title: '正在游戏',
    timeout1: '',
    timeout2: '',
    acid: '',
    usertype: '',
    mapscale: '',
    markers: [],
    poi: {
      latitude: 39.945773,
      longitude: 116.479862,
    },
    lat: '',
    long: '',
    big: false,
    left: false,
    groupid: '',
    leftteam: [],
    rightteam: [],
    teams: [],
    touch: {
      distance: 0,
      scale: 0.5,
      baseWidth: null,
      baseHeight: null,
      scaleWidth: null,
      scaleHeight: null
    },
    pointindex: pointindex,
    width: wx.getSystemInfoSync().windowWidth,
    height: wx.getSystemInfoSync().windowHeight,
    shoo: true,
    muban:1,
    two: [{
      img: 'https://www.juquduo.com/static/resourse/utils/moban.png',
      show: false,
      title: '故事背景',
      content: '公元208年，曹操南下夺取荆州，刘备只好带着百姓逃往江夏。当刘备一行人到达当阳县时，被追赶的曹军包围。经过一番厮杀，刘备等人才摆脱曹军的追赶。此时，与刘备走散的赵云正带着三四十名手下在乱军中寻找刘备妻儿',
      renwu: ''
    },
    {
      img: 'https://www.juquduo.com/static/resourse/utils/moban.png',
      show: false,
      title: '故事背景',
      content: '曹兵马上就追上来了，护送主公刘备到达地图中标记的1号点位。',
      renwu: ''
    },
  ],
  content:{}
    
  },
  next(){
    var that = this;
    that.setData({
      content:that.data.two[1]
    })
  },
  onLoad: function (options) {
    var that = this;
    if(pointindex==0){
      that.setData({
        content:that.data.two[0]
      })
    }else{
      that.setData({
        muban:5
      })
    }

    openid = wx.getStorageSync('openid')
    // 页面第一次加载时获取options参数
    if (options.site) {
      if (options.id == '256' || options.id == '271' || options.id == '275') {
        anniunext = true
      }
      // 有groupid，团建游戏
      if (options.groupid) {
        groupid = options.groupid
        loadurl = 'https://www.juquduo.com/juquduo/gaming/loading'
        console.log('this里的初始值groupid', this.data.groupid) //为空，不是undefined   
        this.setData({
          height: wx.getSystemInfoSync().windowHeight,
          width: wx.getSystemInfoSync().windowWidth,
          groupid: groupid
        })
        // 有roomid，房建游戏
      } else if (options.roomid) {
        roomid = options.roomid, fenzu = options.fenzu
        loadurl = 'https://www.juquduo.com/juquduo/gaming/constructed/loading'
        console.log('this里的初始值roomid', this.data.roomid) //为空，不是undefined   
        this.setData({
          height: wx.getSystemInfoSync().windowHeight,
          width: wx.getSystemInfoSync().windowWidth,
          roomid: roomid
        })
        //都没有，单人游戏
      } else {
        loadurl = 'https://www.juquduo.com/juquduo/gaming/privy/loading'
      }
      // roomid = '5fb237e9',fenzu = 1
      console.log('组id、房间id', groupid, roomid)
      sdsdid = options.id;
      sitesite = options.site;
      fanwei = 0.000201
      wx.setStorageSync('acidacid', sdsdid)
      if (options.site == 1) {
        sitesite = 1
        this.setData({
          poi: { //朝阳公园
            latitude: 39.945773,
            longitude: 116.479862,
          },
          acid: sdsdid,
          usertype: 1,
          mapscale: 14.6
        })
      }
      if (options.site == 2) {
        sitesite = 2
        this.setData({
          poi: { //附近
            latitude: 39.958121,
            longitude: 116.46656,
          },
          acid: sdsdid,
          usertype: 1,
          mapscale: 17.3
        })
      }
      if (options.site == 3) {
        sitesite = 3
        this.setData({
          poi: { //水长城，acid:155
            latitude: 40.410951,
            longitude: 116.310284,
          },
          acid: sdsdid,
          usertype: 1,
          mapscale: 14.6
        })
      }
      if (options.site == 4) {
        sitesite = 4
        this.setData({
          poi: { //古北水镇，acid:269
            latitude: 40.648427,
            longitude: 117.273023,
          },
          acid: sdsdid,
          usertype: 1,
          mapscale: 15.6
        })
      }
      if (options.site == 5) {
        sitesite = 5
        this.setData({
          poi: { //古北水镇，acid:269
            latitude: 40.947638,
            longitude: 115.437874,
          },
          acid: sdsdid,
          usertype: 1,
          mapscale: 15.6
        })
      }
      if (sdsdid == 271 || sdsdid == 269 || sdsdid == 275) {
        // sitetype='tree',sitetype = 1，sitetype为1，为树形点位
        sitetype = 1
      } else {
        // sitetype='line'
        sitetype = 2
      }
      this.setData({
        sitetype: sitetype
      })
    }
    // this.showxiansuo()
    // console.log('地图检测范围',fanwei,'游戏页面的活动id',sdsdid)
    // 如果site为1，显示朝阳公园，因为有options,所以不能放到onshow里面
    // 获取的上个页面传的site之后，赋给全局变量，因为下一次进入页面时，没有传递site、acid参数，只触发一次
    // 有了site就可以确定地图中心点、地图放大倍数，所以判断site

    var that = this
    all = this.data.all
    //sitetype==2，单主线类型，太舞小镇，活动id256、257
    if (sitetype == 2) {
      // 加载游戏信息
      wx.request({
        // url: 'https://www.juquduo.com/juquduo/start_activity/loading_points',
        url: loadurl,
        data: {
          openid: 'oQUXz5IDxMpQcBvHaUayzE7Hy0R0', // openid:'oQUXz5IDxMpQcBvHaUayzE7Hy0R0',
          activityId: sdsdid, // activityId:126,
          isRelease: 3,
          roomId: roomid,
        },
        success(res) {
          that.setData({
            all: res.data
          })
          if (res.code == 100) {
            wx.showToast({
              title: '暂无数据，请稍后再试',
              icon: 'none',
            })
          }
          // all用于传递点位信息，获取all的长度
          var str = res.data[0].PlayerRecord
          var str1 = res.data[0].MyMistake
          // console.log(str,str1)
          // if(str)
          var temppid1 = 0
          var temppid2 = 0
          for (var i = 1; i < 8; i++) {
            if (str.indexOf(i + '') > -1) {
              // if(str.indexOf(i+'.1')>-1){
              temppid1 = i
              // console.log('temppid1',temppid1)
            }
            if (str1.indexOf(i + '') > -1) {
              // if(str1.indexOf(i+'.1')>-1){
              temppid2 = i
              // console.log('temppid2',temppid2)
            }
          }
          temppid = (temppid1 > temppid2) ? temppid1 : temppid2
          // pointindex = temppid
          console.log('返回值里的主线关', temppid)
          //一、先排序，因为markers显示要有序，，每次都要获取返回值，用来
          console.log('openid', openid, 'acid', sdsdid, )
          var allll = []
          all = res.data
          // that.setData({all:res.data})
          aalength = all.length
          for (var i = 0; i < aalength; i++) {
            for (var y = 0; y < aalength; y++) {
              var ppoid = res.data[i].BranchId
              if (ppoid == 1 + y) {
                allll[y] = res.data[i]
              }
            }
          }
          all = allll

          console.log('排序', all)
          var newliea = []
          var point = all
          var y = 0
          //把服务器返回的值改一下属性名，用于markers显示
          for (var i = 0; i < point.length; i++) {
            // for (var i = point.length-1; i >-1; i--) {
            y = y + 1
            var id = point[i].BranchId;
            var latitude = point[i].Lat;
            var longitude = point[i].Lng
            var iconPath = tubiao;
            var width = '45';
            var height = '35'
            // var content = '0'+y; 
            var content = point[i].BranchName;
            var label = new Array;
            //给标记点上添加文字
            label.push({
              'content': content,
              'color': '#ffffff',
              'anchorX': '-7',
              'anchorY': '-32'
            })
            var item = {
              id: id,
              latitude: latitude,
              longitude: longitude,
              iconPath: iconPath,
              width: width,
              height: height,
              label: label[0]
            };
            newliea = newliea.concat(item);
          }
          alldd = newliea
          // that.setData({markers:alldd})
          console.log('markers显示的数组', alldd)

          // 二、判断是否有未完成游戏，如果有，从2个数组中获取任务进度，
          console.log('断线重连的判断参数', pointindex, temppid)
          if (pointindex == 0 && temppid > 0) {
            wx.showModal({
              title: '检测到有未完成游戏，是否继续？',
              showCancel: false,
              success(res2) {
                if (res2.confirm) {
                  pointindex = temppid
                  // 重连，判断2个数组，有值的话，改一下list，这样才能地图不检测，改一下pointindex(主线关的标记)
                  var str = all[1].PlayerRecord
                  var str2 = all[1].MyMistake
                  list = []
                  for (var i = 0; i < all.length; i++) {
                    // console.log(i,all[i].BranchName)
                    // str里面包含all里的1.2，只要里面有，就不检测，后面的判断没有用
                    if (str.indexOf(all[i].BranchName) > -1 && all[i].BranchName <= temppid + 1.1) {
                      list[i] = '2'
                      // console.log('list加的下标--有得分',i)
                    }
                    // 只要里面有，就不检测
                    if (str2.indexOf(all[i].BranchName) > -1 && all[i].BranchName <= temppid + 1.1) {
                      list[i] = '2'
                      // console.log('list加的下标--得分0',i)
                    }
                  }
                  //如果继续游戏,xiansuo11需要重置
                  var str = res.data[0].PlayerRecord
                  for (var i = 1; i < 8; i++) {
                    // console.log('循环次数',i)
                    // i如果最后等于1，说明1.1已经过了，要开始检测2.1的地图，pid如果是1，可以正常检测地图
                    if (str.indexOf(i + '.1') > -1) {
                      pointindex = i
                      // console.log('已经过了的主线',pointindex)
                    }
                  }
                  that.addxiansuo()
                  console.log('重连-pid', pointindex)
                  // 传给下个页面的信息，现在没有问题
                  var backmsg = all[pointindex]
                  wx.setStorageSync('pointmsg', backmsg);
                  pointindextmp = pointindex
                  wx.startLocationUpdate({})
                  wx.onLocationChange(_locationChangeFn)

                } else {
                  console.log('重新开始游戏，pid', pointindex)
                  pointindextmp = pointindex
                  that.addxiansuo()
                  wx.startLocationUpdate({})
                  wx.onLocationChange(_locationChangeFn)
                }
              }
            })
          } else {
            console.log('不是断线重连', temppid)
            pointindex = temppid
            pointindextmp = pointindex
            that.addxiansuo()
            wx.startLocationUpdate({})
            wx.onLocationChange(_locationChangeFn)
          }

          //三、开始把markers显示到页面上，，加载第一个点的标记
          if (pointindex == 0) {
            console.log('加载第一个点，pid=0')
            mks.push(alldd[0])
          }
          // that.setData({markers:mks})
        }
      })
      //四、位置变化后的逻辑，页面跳转、传参数
      _locationChangeFn = function (res) {
        // 检测位置，用于触发关卡
        wx.getLocation({
          type: 'gcj02',
          success(ress) {
            const latitude = ress.latitude
            const longitude = ress.longitude
            const speed = ress.speed
            const accuracy = ress.accuracy
            that.setData({
              lat: latitude,
              long: longitude
            })
          }
        })
        // console.log('ditu检测的shuju',pointindex,pointindextmp)
        for (var i = 0; i < aalength; i++) {
          // all和alldd都是排序过的，获取i的值可以获取对应alldd里的下标,list里面有值的，就要显示对应alldd的markers假设pid是1，检测到的是1.3或2.1，参数iaaa是1.3对应的下标
          if (all[i].BranchName <= temppid + 1.1 && list[i] != '2') {
            // console.log('判断的点的列表：',i)
            if (Math.abs(that.data.lat - alldd[pointindex].latitude) < fanwei && Math.abs(that.data.long - alldd[pointindex].longitude) < fanwei) {
              //这个if每次都会触发，目的是触发一次
              if (panduan == 1) {
                console.log('检测到的下标、分支名是', i, all[i].BranchName)
                iaaa = i
                list[iaaa] = '2' //过的点的下标赋给list，用来判断地图
                // BranchId为1，是主线关卡，pid++
                // if(all[iaaa].BranchId==1){
                pointindex++
                // }
                xiansuo11++
                wx.setStorageSync('pointnum', pointindex) //路标页面会取这个值
                mks.push(alldd[iaaa])
                panduan = 2
                //获取对应下标的msg，传到下一个页面 
                var msg = all[iaaa];
                // msg.BranchType==3
                // BranchType为2是问题，显示问题页面
                if (msg.BranchType == 2) {
                  wx.redirectTo({
                    // 跳转的路标文件的序号是固定的，因为放大倍数不一样，pointindex改成了主线关卡数，
                    url: '../dgame2/dgame2?pid=' + pointindex + '&acid=' + sdsdid + '&sitetype=' + sitetype,
                  })
                  wx.setStorageSync('pointmsg', msg); //这个本地存储的内容、现在只有怪兽页面会取一下，问题页面用随机数、线索页面用数组下标
                }
                // BranchType为3是怪物，显示怪物页面
                if (msg.BranchType == 3) {
                  wx.showModal({
                    title: '雷达探测到附近有怪兽出没\r\n请打开摄像头寻找！',
                    showCancel: false,
                    success() {
                      wx.redirectTo({
                        url: '../animate1/index?pid=' + pointindex + '&sitetype=' + sitetype,
                      })
                    }
                  })
                  wx.setStorageSync('pointmsg', msg);
                }
              }
              wx.stopLocationUpdate({
                success: (res) => {
                  console.log('停止监听位置改变-11')
                },
              })
              // 2.判断最后一关，路标显示完之后，退出游戏
              if (pointindex >=9) {
                wx.setStorageSync('isover', true)
              } else {
                wx.setStorageSync('isover', false)
              }
            }
          }
        }
      }
    }



    //sitetype==1，树形主线类型，活动id269，271，275
    if (sitetype == 1) {
      wx.request({
        // url: 'https://www.juquduo.com/juquduo/start_activity/loading_points',
        url: loadurl,
        data: {
          openid: openid, // openid:'oQUXz5IDxMpQcBvHaUayzE7Hy0R0',
          activityId: sdsdid, // activityId:126,
          isRelease: 3,
          roomId: roomid,
        },
        success(res) {
          console.log(res)
          that.setData({
            all: res.data
          })
          if (res.code == 100 || res.data.length < 3) {
            wx.showToast({
              title: '暂无数据，稍后再试',
              icon: 'none',
            })
          }
          // all用于传递点位信息，获取all的长度
          var str = res.data[0].PlayerRecord
          var str1 = res.data[0].MyMistake
          // if(str)
          var temppid1 = 0
          var temppid2 = 0
          for (var i = 1; i < 9; i++) {
            if (str.indexOf(i + '.1') > -1) {
              temppid1 = i
              // console.log('temppid1',temppid1)
            }
            if (str1.indexOf(i + '.1') > -1) {
              temppid2 = i
              // console.log('temppid2',temppid2)
            }
          }
          temppid = (temppid1 > temppid2) ? temppid1 : temppid2
          // pointindex = temppid
          console.log('返回值里的主线关', temppid)
          //一、先排序，因为markers显示要有序，，每次都要获取返回值，用来
          console.log('openid', openid, 'acid', sdsdid, )
          all = res.data
          aalength = all.length
          console.log('没有排序', all)

          var newliea = []
          var point = all
          var y = 0
          //把服务器返回的值改一下属性名，用于markers显示
          for (var i = 0; i < point.length; i++) {
            // for (var i = point.length-1; i >-1; i--) {
            y = y + 1
            console.log(point)
            var id = point[i].PointId; //latitude
            var longitude = point[i].Lat;
            var latitude = point[i].Lng
            var iconPath = tubiao;
            var width = '45';
            var height = '35'
            // var content = '0'+y; 
            var content = point[i].BranchName;
            var label = new Array;
            //给标记点上添加文字
            label.push({
              'content': content,
              'color': '#ffffff',
              'anchorX': '-7',
              'anchorY': '-32'
            })
            var item = {
              id: id,
              latitude: latitude,
              longitude: longitude,
              iconPath: iconPath,
              width: width,
              height: height,
              label: label[0]
            };
            newliea = newliea.concat(item);
          }
          alldd = newliea
          // that.setData({markers:alldd})
          console.log('markers显示的数组', alldd)

          // 二、判断是否有未完成游戏，如果有，从2个数组中获取任务进度，
          console.log('断线重连的判断参数', pointindex, temppid)
          if (pointindex == 0 && temppid > 0) {
            wx.showModal({
              title: '检测到有未完成游戏，是否继续？',
              showCancel: false,
              success(res2) {
                if (res2.confirm) {
                  pointindex = temppid
                  // 重连，判断2个数组，有值的话，改一下list，这样才能地图不检测，改一下pointindex(主线关的标记)
                  var str = all[1].PlayerRecord
                  var str2 = all[1].MyMistake
                  list = []
                  for (var i = 0; i < all.length; i++) {
                    // console.log(i,all[i].BranchName)
                    if (str.indexOf(all[i].BranchName) > -1 && all[i].BranchName <= temppid + 1.1) {
                      list[i] = '2'
                      // console.log('list加的下标--有得分',i)
                    }
                    if (str2.indexOf(all[i].BranchName) > -1 && all[i].BranchName <= temppid + 1.1) {
                      list[i] = '2'
                      // console.log('list加的下标--得分0',i)
                    }
                  }
                  //如果继续游戏,xiansuo11需要重置
                  var str = res.data[0].PlayerRecord
                  for (var i = 1; i < 8; i++) {
                    // console.log('循环次数',i)
                    // i如果最后等于1，说明1.1已经过了，要开始检测2.1的地图，pid如果是1，可以正常检测地图
                    if (str.indexOf(i + '.1') > -1) {
                      pointindex = i
                      // console.log('已经过了的主线',pointindex)
                    }
                  }
                  that.addxiansuo()
                  console.log('重连-pid', pointindex)
                  // 传给下个页面的信息，现在没有问题
                  var backmsg = all[pointindex]
                  wx.setStorageSync('pointmsg', backmsg);
                  pointindextmp = pointindex
                  wx.startLocationUpdate({})
                  wx.onLocationChange(_locationChangeFn)

                } else {
                  console.log('重新开始游戏，pid', pointindex)
                  pointindextmp = pointindex
                  that.addxiansuo()
                  wx.startLocationUpdate({})
                  wx.onLocationChange(_locationChangeFn)
                }
              }
            })
          } else {
            console.log('不是断线重连', temppid)
            pointindex = temppid
            pointindextmp = pointindex
            that.addxiansuo()
            wx.startLocationUpdate({})
            wx.onLocationChange(_locationChangeFn)
          }

          //三、开始把markers显示到页面上，，加载第一个点的标记
          if (pointindex == 0) {
            console.log('加载第一个点，pid=0')
            mks.push(alldd[0])
          }
          // that.setData({markers:mks})
        }
      })
      //四、位置变化后的逻辑，页面跳转、传参数
      _locationChangeFn = function (res) {
        // 检测位置，用于触发关卡
        wx.getLocation({
          type: 'gcj02',
          success(ress) {
            const latitude = ress.latitude
            const longitude = ress.longitude
            const speed = ress.speed
            const accuracy = ress.accuracy
            that.setData({
              lat: latitude,
              long: longitude
            })
          }
        })
        // console.log('pointindex',pointindex)
        for (var i = 0; i < aalength; i++) {
          // console.log(aalength)
          // console.log(pointindex)
          // console.log('sjj')
          // if(panduan == 1){
            if(pointindex == 0 && all[i].BranchName < 2 ){
            if (Math.abs(that.data.lat - alldd[i].latitude) < fanwei && Math.abs(that.data.long - alldd[i].longitude) < fanwei) {
              console.log('0',all[i].BranchName)
              if(panduan == 1){
                pointindex++
                panduan=2
                iaaa = i
                list[iaaa] = '2'
                xiansuo11++
                wx.setStorageSync('pointnum', pointindex)
                mks.push(alldd[iaaa])
              var msg = all[iaaa];
              wx.setStorageSync('pointmsg', msg);
              wx.showModal({
                title: '显示模板',
                showCancel: true,
                
            success(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: './new?pointindex='+pointindex +'&sitetype=' + sitetype+'&i='+i,
                })
              }
            }
            })
                wx.stopLocationUpdate({
                  success: (res) => {
                    console.log('停止监听位置改变-11',pointindex)
                  }
                })
              }
            }
          }
          else if(pointindex == 1 && (all[i].BranchName > 2 && all[i].BranchName < 3)){
            if (Math.abs(that.data.lat - alldd[i].latitude) < fanwei && Math.abs(that.data.long - alldd[i].longitude) < fanwei) {
              // console.log('1',all[i].BranchName)
              console.log('打印的点位',all[i].BranchName)
              if(panduan == 1){
              pointindex++
              panduan=2
              iaaa = i
              list[iaaa] = '2'
              xiansuo11++
              wx.setStorageSync('pointnum', pointindex)
              mks.push(alldd[iaaa])
              var msg = all[iaaa];
              wx.setStorageSync('pointmsg', msg);
              wx.showModal({
                title: '显示模板',
                showCancel: true,
                success(res) {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: './new?pointindex='+pointindex+ '&sitetype=' + sitetype+'&i='+i,
                    })
                  }
                }
              })
              wx.stopLocationUpdate({
                success: (res) => {
                  console.log('停止监听位置改变-11',pointindex)
                }
              })
              }
            }
          }
          else if(pointindex > 1 && all[i].BranchName > 3  && list[i] != '2'){
            if (Math.abs(that.data.lat - alldd[i].latitude) < fanwei && Math.abs(that.data.long - alldd[i].longitude) < fanwei) {
              console.log('2',all[i].BranchName)
              
              if(panduan == 1){
              iaaa = i
              if(all[iaaa].BranchId==1){
                pointindex++
              }
              panduan=2 


              list[iaaa] = '2'
              xiansuo11++
              console.log('打印的点位',all[i].BranchName)
              wx.setStorageSync('pointnum', pointindex)
              mks.push(alldd[iaaa])
              var msg = all[iaaa];
              wx.setStorageSync('pointmsg', msg);
              wx.showModal({
                title: '显示模板',
                showCancel: true,
                success(res) {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: './new?pointindex='+pointindex+ '&sitetype=' + sitetype+'&i='+i,
                    })
                  }
                }
              })
              wx.stopLocationUpdate({
                success: (res) => {
                  console.log('停止监听位置改变-11',pointindex)
                }
              })

              if(pointindex>=8){
                wx.setStorageSync('isover', true)
              }else{
                wx.setStorageSync('isover', false)
              }
            }
            }
          }
        // }
        }
      }
    }
    wx.setNavigationBarTitle({
      title: '正在游戏',
    })
  },
  onReady() {
    console.log('list', list, 'all', all, 'alldd', alldd, 'mks', mks,
      'aalength', aalength, 'openid', openid, 'xsarr', xsarr, 'fanwei', fanwei, 'groupid', groupid, 'roomid', roomid, 'fenzu', fenzu,
      'pointindex', pointindex, 'pointindextmp', pointindextmp, 'temppid', temppid,
      'xiansuo11', xiansuo11, 'panduan', panduan,
      'sitesite', sitesite, 'sdsdid', sdsdid, 'iaaa', iaaa, 'sitetype', sitetype, )
  },
  showbutton() {
    this.setData({
      isshowbtn: !this.data.isshowbtn
    })
  },
  addxiansuo() {
    console.log('temppid', temppid, 'sitetype', sitetype)
    // 任务列表数组，显示到页面
    var str = all[1].PlayerRecord
    var str2 = all[1].MyMistake
    xsarr = [], mks = []
    // 任务全都显示，已经过了的改样式
    console.log('线索循环之前的all', all)
    if (temppid == undefined) {
      temppid = 0
    }
    for (var i = 0; i < all.length; i++) {
      // 这是要显示的线索
      // if(all[i].BranchName>1&&all[i].BranchName<2&&all[i].BranchName <= temppid + 1.3){
      //   all[i].BranchName
      //   if(all[i].BranchName=='1.1'){
      //      xsarr[0]=all[0]
      //      i=0
      //   }else if(all[i].BranchName=='1.2'){
      //     xsarr[0]=all[1]
      //     i=1
      //   }else if(all[i].BranchName=='1.3'){
      //     xsarr[0]=all[2]
      //     i=2
      //   }
      //   console.log('str2'.str2)
      //   // xsarr[0].passed='2'
      //   if (str.indexOf(all[i].BranchName) > -1) {
      //     xsarr[0].passed = '1'
      //     mks.push(alldd[i])
      //     // 如果str里面没有，str2里面有，passed改为3，添加标记
      //   } else if (str2.indexOf(all[i].BranchName) > -1) {
      //     xsarr[0].passed = '3'
      //     mks.push(alldd[i])
      //   } else{
      //     xsarr[0].passed = '2'
      //   }
      // }else if(all[i].BranchName>2&&all[i].BranchName<3&&all[i].BranchName <= temppid + 1.3){
      //   if(all[i].BranchName=='2.1'){
      //     xsarr[1]=all[3]
      //  }else if(all[i].BranchName=='2.2'){
      //    xsarr[1]=all[4]
      //  }else if(all[i].BranchName=='2.3'){
      //    xsarr[1]=all[5]
      //  }
      //   if (str.indexOf(all[i].BranchName) > -1) {
      //     xsarr[1].passed = '1'
      //     mks.push(alldd[i])
      //     // 如果str里面没有，str2里面有，passed改为3，添加标记
      //   } else if (str2.indexOf(all[i].BranchName) > -1) {
      //     xsarr[1].passed = '3'
      //     mks.push(alldd[i])
      //   } else {
      //     // 如果都没有，是没有过的支线，passed改为2。改为2的作用：用来显示在任务板，list数组用来判断地图
      //     xsarr[1].passed = '2' //已通过的点，改为2，根据2，改变样式
      //     //已通过的是错的点，样式不变，改分数文字 
      //   }
      // }else 
      if(all[i].BranchName>1&&all[i].BranchName <= temppid + 1.1) {
        xsarr[i] = all[i]
        // 如果str里面有，passed改为1，添加标记
        if (str.indexOf(all[i].BranchName) > -1) {
          xsarr[i].passed = '1'
          mks.push(alldd[i])
          // 如果str里面没有，str2里面有，passed改为3，添加标记
        } else if (str2.indexOf(all[i].BranchName) > -1) {
          xsarr[i].passed = '3'
          mks.push(alldd[i])
        } else {
          // 如果都没有，是没有过的支线，passed改为2。改为2的作用：用来显示在任务板，list数组用来判断地图
          xsarr[i].passed = '2' //已通过的点，改为2，根据2，改变样式
          //已通过的是错的点，样式不变，改分数文字 
        }
      }
      //如果过了任务，样式改为1
    }
    // console.log(xsarr,mks)
    // this.setData({xsarr:xsarr})
    console.log('线索数组长度，sitetype', xsarr.length, this.data.sitetype, xsarr)
    this.setData({
      xsarr: xsarr
    }, () => {
      console.log('线索赋值成功')
    })
  },
  onShow() {
    var _this = this;
    panduan = 1
    var all = this.data.all


    _this.setData({
      anniunext: anniunext,
      sitetype: sitetype
    })
    // _this.setData({markers:mks,anniunext:anniunext})
    console.log('页面显示时的覆盖物', mks)
    console.log('sitesite', sitesite)
    if (sitesite == 1) {
      this.setData({
        poi: { //朝阳公园
          latitude: 39.945773,
          longitude: 116.479862,
        },
        acid: sdsdid,
        usertype: 1,
        mapscale: 14.6
      })
    }
    if (sitesite == 2) {
      this.setData({
        poi: { //附近
          latitude: 39.958121,
          longitude: 116.46656,
        },
        acid: sdsdid,
        usertype: 1,
        mapscale: 17.3
      })
    }
    if (sitesite == 3) {
      this.setData({
        poi: { //水长城
          latitude: 40.410951,
          longitude: 116.310284,
        },
        acid: sdsdid,
        usertype: 1,
        mapscale: 14.6
      })
    }
    if (sitesite == 4) {
      this.setData({
        poi: { //古北水镇
          latitude: 40.648427,
          longitude: 117.273023,
        },
        acid: sdsdid,
        usertype: 1,
        mapscale: 15.6
      })
    }
    if (sitesite == 5) {
      this.setData({
        poi: { //云顶天宫
          latitude: 40.947638,
          longitude: 115.437874,
        },
        acid: sdsdid,
        usertype: 1,
        mapscale: 15.6
      })
    }
    // this.addxiansuo()
    // _this.setData({markers:mks})
    console.log('第', xiansuo11, '次显示页面')
  },
  xiansuo() {
    this.setData({
      xiansuo: 'https://www.juquduo.com/static/resourse/game4/xiansuo_an.png'
    })
  },
  //点击出现左下角小地图
  qiehuan() {
    wx.redirectTo({
      url: '../dgame1dt/index?site=' + sitesite
    })
  },
  changebtn() {
    this.setData({
      xiansuo: 'https://www.juquduo.com/static/resourse/game4/xiansuo_an.png'
    })
  },
  showxiansuo() {
    // this.setData({isshowxiansuo2:true})
    // console.log(BranchName)
    this.setData({
      isshowxiansuo2: true,
      showmap: false,
      showbotbar: false
    }) //树形
    // var xiansuomsg = wx.getStorageSync('pointmsg')
    var xiansuomsg = all[pointindex]
    // if(pointindex==0){
    //   xiansuomsg='请走到起点获取线索'
    // }

    // console.log(xsarr)
    // this.setData({xiansuocontent:xiansuomsg.Clue})
    // if(xiansuomsg.RightClue==''){
    //   this.setData({xiansuocontent:xiansuomsg.Description})
    // }

  },
  hidexiansuo() {
    this.setData({
      isshowxiansuo2: false,
      showmap: true,
      showbotbar: true,
      xiansuo: 'https://www.juquduo.com/static/resourse/game4/xiansuo.png'
    })
  },
  zhuye() {
    wx.redirectTo({
      url: '../ayeshou/yeshou',
    })
  },
  //切换底部导航
  mapbtn() {
    this.setData({
      showmap: true,
      showzhankuang: false,
      game1title: '正在游戏'
    })
  },
  scobtn() {
    if (groupid != '') {
      this.setData({
        showzhankuang: true
      })
    }
    if (roomid != '') {
      this.setData({
        roomscore: true
      })
    }
    this.setData({
      showmap: false,
      game1title: '战况'
    })
    var that = this
    wx.setNavigationBarTitle({
      title: '战况',
    })
    // url: 'https://www.juquduo.com/juquduo/gaming/group_score',
    // /juquduo/battle/team_battle?openid=oQUXz5JGOodzUTbbl8oabLJZBu88&activityId=264
    if (groupid != '') {
      wx.request({
        url: 'https://www.juquduo.com/juquduo/battle/team_battle',
        data: {
          activityId: sdsdid,
          // activityId:259,
          openid: openid
        },
        success(res) {
          console.log('战况：', res.data.GroupsRank)
          // console.log(res.data.Data[1])
          var scoremsg = res.data.GroupsRank
          for (var i = 0; i < scoremsg.length; i++) {
            for (var j = 0; j < scoremsg.length; j++) {
              if (scoremsg[i].GroupScore > scoremsg[j].GroupScore) {
                var temp = scoremsg[i]
                scoremsg[i] = scoremsg[j]
                scoremsg[j] = temp
              }
            }
          }
          that.setData({
            teams: scoremsg
          })
        }
      })
    }
    if (roomid != '') {
      wx.request({
        url: 'https://www.juquduo.com/juquduo/gaming/constructed/battle',
        data: {
          activityId: 257,
          // roomId:roomid,
          roomId: '5fb1e13a',
          // role:fenzu
          role: 1
        },
        success(res) {
          // 这里要改成给每个人加成绩，界面要用之前的
          console.log('战况：', res.data.Mine[0])
          var myscore = res.data.Mine[0]
          that.setData({
            leftteam: myscore.Mine,
            rightteam: myscore.Other
          })
        }
      })
    }
  },
  onHide() {
    var that = this
    console.log('onHide触发')
    console.log('定时器已清除')
  },
  changefanwei() {
    var that=this;
    fanwei = 2.010101
    console.log('点击位置检测',pointindex)
    setTimeout(function () {
      console.log('检测范围变小')
      fanwei = 0.000201
    }, 3000)
    // console.log(PlayerRecord)
  },
  onUnload() {
    var that = this
    console.log('onHide触发')
    console.log('定时器已清除')
  },
  showrulor() {
    if (this.data.showrrr) {
      this.setData({
        showrrr: false
      })
    } else {
      this.setData({
        showrrr: true
      })
    }
  },
  tuichuyouxi() {
    var that=this;
    wx.showModal({
      // showCancel:true,
      // cancelText:'返回',
      // cancelColor: 'black',
      title: '是否确定退出游戏？',
      // confirmColor:'#ff0000',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.juquduo.com/juquduo/gaming/privy/exit',
            data: {
              activityId: sdsdid,
              openid: openid,
              groupid: groupid
            },
            success(res) {
              
              console.log('退出的参数', sdsdid, openid)
              console.log('退出按钮', res)
              pointindex = 0, temppid = 0
              xiansuo11 = 1, groupid = '', roomid = ''
              that.setData({
                muban:5
              })
              xsarr = [], list = [], mks = []
              wx.switchTab({
                url: '../aacplace/aacplace',
              })
              // console.log(pointindex)
              console.log(xsarr)
            }
          })
        }
      }
    })
  }
})